package com.ai.chinamobileai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.function.Consumer;

@RequestMapping("/ai")
@RestController
public class MyChatController {

    @Autowired
    private ChatClient jsonClient;
//保存chatId
@PostMapping(value = "/chat",produces = "text/html;charset=UTF-8")
public Flux<String> stream(String prompt, String chatId){
    return jsonClient.prompt(prompt)
            .advisors(new Consumer<ChatClient.AdvisorSpec>() {
                @Override
                public void accept(ChatClient.AdvisorSpec advisorSpec) {
                    advisorSpec.param(ChatMemory.CONVERSATION_ID,chatId);
                }
            })
            .stream()
            .content();
}
}
